﻿<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
/**
 * 写病人预约信息 对应的存储过程名称 -》写病人预约信息
 */
	public Message addPatientAppoint(Map<String, String> param) {
		Connection connection = null;
		java.sql.CallableStatement statement = null;
		Message msg = new Message(0, "操作成功");
		java.sql.ResultSet rs = null;
		try {
			connection = getConn();
			statement = connection.prepareCall("{ call 写病人预约信息(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
			statement.setString(1, "04");
			statement.setString(2, Tools.getDesc(param.get("patid"), ""));
			statement.setString(3, Tools.getDesc(param.get("blh"), "")); 
			statement.setString(4, Tools.getDesc(param.get("hzxm"), ""));
			statement.setString(5, Tools.getDesc(param.get("pbkm"), ""));
			statement.setString(6, Tools.getDesc(param.get("deptName"), ""));
			statement.setString(7,Tools.getDesc(param.get("yylx"), ""));
			Calendar date = Calendar.getInstance();
			String year     = String.valueOf(date.get(Calendar.YEAR));  
			statement.setString(8, year);
			statement.setString(9,  "20");
			statement.setString(10,Tools.getDesc(param.get("pbmxid"), ""));
			statement.setString(11,"-1");
			statement.setString(12, "1");
			statement.setString(13, "00");
			statement.setString(14,Tools.getDesc(param.get("yyrq"), ""));
			statement.setString(15, "0");
			statement.setString(16, "");
			statement.setString(17,Tools.getDesc(param.get("yyTime"), ""));
			statement.setString(18, "");
			statement.setString(19,"");
			statement.setString(20, "");
			statement.setString(21, "1");
			statement.setString(22, "");
			statement.setString(23, "0");         
	 		rs = statement.executeQuery();
			String state = "";
			String message = "";
			while(rs.next()){
				state=rs.getString(1);
				message=rs.getString(2);
			}
			if("F".equalsIgnoreCase(state)) {
				msg.put("errcode",FAILURE);
				msg.put("errmsg",message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if(rs != null)
						rs.close();
					if (statement != null)
						statement.close(); 
					if (connection != null)
						connection.close(); 
				} catch (Exception e) {
					rs=null;
					statement=null;
					connection=null;
					e.printStackTrace();
				}
			return msg;
		}
	}
	
	/**
 * 写病人基本信息 对应的存储过程名称 -》写病人基本信息
 */
	public Message addPatientInfo(Map<String, String> param) {
		Connection connection = null;
		CallableStatement statement = null;
		Message msg = new Message(0, "操作成功");
		ResultSet rs = null;
		try {
			connection = getConn();
			statement = connection.prepareCall("{call 写病人基本信息(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			
			//statement.setString(1, Tools.getDesc(param.get("blh"), ""));
			statement.setString(1, ""); //病人病历号
			statement.setString(2, param.get("sickName")); //姓名
			statement.setString(3, "00"); //操作员(最好就用00，这是系统超级用户)
			statement.setString(4,  "1"); //卡类别	1:磁卡
			statement.setString(5, Tools.getDesc(param.get("cardNum"), "")); //卡号
			statement.setString(6,  ""); //个人医保号
			statement.setString(7,  ""); //单位医保号
			statement.setString(8,  ""); //社保号
			statement.setString(9,  ""); //其它卡号
			statement.setString(10, Tools.getDesc(param.get("sfzh"), ""));          // 身份证号
			statement.setString(11, Tools.getDesc(param.get("sex"), ""));           // 性别
			statement.setString(12, Tools.getDesc(param.get("birth"), ""));          //出生年月
			statement.setString(13, Tools.getDesc(param.get("lxdz"), ""));          //联系地址
			statement.setString(14, Tools.getDesc(param.get("lxdh"), ""));          //联系电话（必填）
			statement.setString(15, "");                                           //邮政编码               
			statement.setString(16,  "");                                          //联系人                
			statement.setString(17,  "1");                                          //医保代码（固定）           
			statement.setString(18,  "");           																//凭证号                
			statement.setString(19, "");          																	//单位代码               
			statement.setString(20,  "");                                          //单位名称               
			statement.setString(21, "");                                          //区县代码               
			statement.setString(22, "");                                          //开始日期               
			statement.setString(23, "");                                          //结束日期               
			statement.setString(24, "");                                          //医疗项目               
			statement.setString(25, "");                                          //诊断代码    
			statement.setDouble(26, 0);                                       //帐户金额    
			statement.setString(27, "");                                          //备注      
		 	rs = statement.executeQuery();
			String state = "";
			String message = "";
			while(rs.next()){
				state=rs.getString(1);
				message=rs.getString(2);
			}
			if("F".equalsIgnoreCase(state)) {
				msg.put("errcode",FAILURE);
				msg.put("errmsg",message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
					if(rs != null)
						rs.close();
					if (statement != null)
						statement.close(); 
					if (connection != null)
						connection.close(); 
				} catch (Exception e) {
					rs=null;
					statement=null;
					connection=null;
					e.printStackTrace();
				}
			return msg;
		}
	}
%>

<%
		Message msg = new Message();            
      String cardNo    = Tools.getDesc(input.getDesc("cardNo", ""));     
			String pbkm     = Tools.getDesc(input.getDesc("pbkm", ""));            
			String deptName     = Tools.getDesc(input.getDesc("deptName", ""));          
			String yylx     = Tools.getDesc(input.getDesc("yylx", ""));               
			String pbmxid     = Tools.getDesc(input.getDesc("pbmxid", ""));            
			String yyrq     = Tools.getDesc(input.getDesc("yyrq", ""));              
			String yyTime     = Tools.getDesc(input.getDesc("yyTime", ""));   
				
		// 根据身份证号查询病历号
		Connection connection = null;
		PreparedStatement  statement = null;

		String patid="";
		String blh = "";
		String hzxm="";
		try {
			connection = getConn();
/**
			String sumSql="SELECT count(1) as  from SF_BRXXK where sfzh= ? ";
			statement = connection.prepareStatement(sumSql); 
			statement.setString(1, cardNo);
			ResultSet sumRs = statement.executeQuery();**/
			
		
			String sql="SELECT patid,blh,hzxm from SF_BRXXK where sfzh= ? ";
			statement = connection.prepareStatement(sql); 
			statement.setString(1, cardNo);
			ResultSet rs = statement.executeQuery();
			
			// 如果病人信息卡SF_BRXXK没有记录 则调用写病人基本信息补全 
					/**	if(!rs.next()){ //当结果集不存在时
				
					Message msg2 = new Message();
					try {
							// String blh           =  input.getDesc("blh");      // 病历号       
							String sickName          =  input.getDesc("sickName");     // 姓名        
							String cardNum        =  input.getDesc("cardNum");   //卡号  
							String sfzh          =  input.getDesc("sfzh");     //身份证号（必填）    
							String sex           =  input.getDesc("sex");      //性别    
							String birth         =  input.getDesc("birth");    // 出生日期  
							String lxdz          =  input.getDesc("lxdz");     //联系地址（必填）
							String lxdh          =  input.getDesc("lxdh");     //联系电话（必填）           
						
						//写病人基本信息
							Map<String, String> param2  = new HashMap<String,String>(8);
						//	param2.put("blh",blh);                   
							param2.put("sickName",sickName);                         
							param2.put("cardNum",cardNum);                              
							param2.put("sfzh",sfzh);                 
							param2.put("sex",sex);                   
							param2.put("birth",birth);               
							param2.put("lxdz",lxdz);                 
							param2.put("lxdh",lxdh);                          
							msg2 = addPatientInfo(param2);
							msg2.put("errcode","42011");
							msg2.put("msg","预约失败，请重新预约。");
						} catch (Exception e){
					   	msg2.put("errcode",FAILURE);
				    }
						out.println(msg2);
						return;
			} 
			

			else {
				rs.previous(); // 这段代码有用 请勿删除，在调用next()、first()等方法后，它就会指向第二条，因此在做完判断我们需要给指针前移一位，用previous()这个方法。
			}*/
			
			
			
			
			while(rs.next()){
         patid = rs.getString("patid");
         blh = rs.getString("blh");
         hzxm = rs.getString("hzxm");
      }
     //写病人预约信息
			Map<String, String> param  = new HashMap<String,String>(16);               
			param.put("patid",patid);                 
			param.put("blh",blh);                  
			param.put("hzxm",hzxm);         
			param.put("pbkm",pbkm);             
			param.put("deptName",deptName);               
			param.put("yylx",yylx);               
			param.put("pbmxid",pbmxid); 
			param.put("yyrq",yyrq);               
			param.put("yyTime",yyTime);                           
	     System.out.print("qianzhiji:"+param);       
			msg = addPatientAppoint(param);
		} catch (Exception e){
	   	msg.put("errcode",FAILURE);
    } finally {
			try {
					if (statement != null)
						statement.close(); 
					if (connection != null)
						connection.close(); 
				} catch (Exception e) {
					statement = null;
					connection = null;
					e.printStackTrace();
				}
		}         

		out.println(msg);
%>